package com.atguigu.exam.controller;

import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Banner;
import com.atguigu.exam.service.BannerService;
import com.atguigu.exam.service.FileUploadService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/banners")
@Tag(name = "轮播图管理", description = "轮播图相关操作，包括图片上传、轮播图增删改查、状态管理等功能")
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private FileUploadService fileUploadService;
    @GetMapping("/list")
    @Operation(summary = "获取所有轮播图", description = "获取所有轮播图列表，包括启用和禁用的，供管理后台使用")
    public Result<List<Banner>> getAllBanners(){
        List<Banner> bannerList = bannerService.list();
        return Result.success(bannerList);
    }
    @GetMapping("/active")
    @Operation(summary = "获取启用的轮播图", description = "获取状态为启用的轮播图列表，供前台首页展示使用")
    public Result<List<Banner>> getAllActiveBanners(){
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Banner::getIsActive, true);
        List<Banner> list = bannerService.list(queryWrapper);
        return Result.success(list);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取轮播图", description = "根据轮播图ID获取单个轮播图的详细信息")
    public Result<Banner> getBannerById(@Parameter(description = "轮播图ID") @PathVariable Long id) {
        Banner banner = bannerService.getById(id);
        return Result.success(banner);
    }

    @Operation(summary = "更新轮播图",description = "更新轮播图的信息，包括图片、标题、跳转链接、排序等")
    @PutMapping("/update")
    public Result updateBanner(@Parameter(name = "banner",description = "轮播图信息") @RequestBody Banner banner) {
        boolean r = bannerService.updateById(banner);
        return r ? Result.success("更新成功") : Result.error("更新失败");
    }

    @Operation(summary = "切换轮播图状态",description = "启用或禁用指定的轮播图，禁用后不会在前台显示")
    @PutMapping("/toggle/{id}")
    public Result toggleBanner(@Parameter(name = "id",description = "轮播图ID") @PathVariable Long id,
                               @Parameter(name = "isActive",description = "是否启用，true为启用，false为禁用") @RequestParam Boolean isActive) {
        LambdaUpdateWrapper<Banner> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Banner::getIsActive, isActive).eq(Banner::getId, id);
        boolean r = bannerService.update(updateWrapper);
        return r ? Result.success("更新成功") : Result.error("更新失败");
    }

    @Operation(summary = "添加轮播图",description = "创建新的轮播图，需要提供图片URL、标题、跳转链接等信息")
    @PostMapping("/add")
    public Result saveBanner(@Parameter(name = "banner",description = "轮播图信息") @RequestBody Banner banner) {
        boolean r = bannerService.save(banner);
        return r ? Result.success("添加成功") : Result.error("添加失败");
    }

    @Operation(summary = "删除轮播图", description = "根据ID删除指定的轮播图")
    @DeleteMapping("delete/{id}")
    public Result deleteBannerById(@Parameter(name = "id", description = "轮播图ID") @PathVariable Long id) {
        boolean r = bannerService.removeById(id);
        return r ? Result.success("删除成功") : Result.error("删除失败");
    }

    @Operation(summary = "上传轮播图图片", description = "将图片文件上传到MinIO服务器，返回可访问的图片URL")
    @PostMapping("/upload-image")
    public Result<String> uploadImage(@Parameter(description = "要上传的图片文件，支持jpg、png、gif等格式，大小限制100MB")
                                      MultipartFile file) {
        String url = fileUploadService.uploadFile(file);
        return url.equals("") ? Result.success("上传失败") : Result.success(url,"上传成功");
    }

}
